"ui";

function mainui() {
    ui.layout(
        <frame>
            <scroll>
                <vertical>
                    <vertical bg="file://res/卒业.png">
                        <card bg="#55000000">
                            <vertical>
                                <text text="当前选择修改后照片的背景色:" textColor="#FFFFFF" />
                                <text id="targetcolor" w="90" h="150" bg="{{ '#99'+storage.get('color').substr(1) }}" layout_gravity="center" />
                                <text text="背景色选择:" textColor="#FFFFFF" />
                                <horizontal>
                                    <vertical id="纯黑" w="50" h="90" clickable="true" bg="?android:attr/selectableItemBackground">
                                        <text text="纯黑" textColor="#FFFFFF" textSize="16sp" marginTop="16" gravity="center" />
                                        <text w="50" h="50" bg="#99000000" />
                                    </vertical>
                                    <vertical id="骚粉" w="50" h="90" clickable="true" bg="?android:attr/selectableItemBackground">
                                        <text text="骚粉" textColor="#FFFFFF" textSize="16sp" marginTop="16" gravity="center" />
                                        <text w="50" h="50" bg="#99FFB6C1" />
                                    </vertical>
                                    <vertical id="原谅" w="50" h="90" clickable="true" bg="?android:attr/selectableItemBackground">
                                        <text text="原谅" textColor="#FFFFFF" textSize="16sp" marginTop="16" gravity="center" />
                                        <text w="50" h="50" bg="#993CB371" />
                                    </vertical>
                                    <vertical id="真爱" w="50" h="90" clickable="true" bg="?android:attr/selectableItemBackground">
                                        <text text="真爱" textColor="#FFFFFF" textSize="16sp" marginTop="16" gravity="center" />
                                        <text w="50" h="50" bg="#9900BFFF" />
                                    </vertical>
                                    <vertical id="基佬" w="50" h="90" clickable="true" bg="?android:attr/selectableItemBackground">
                                        <text text="基佬" textColor="#FFFFFF" textSize="16sp" marginTop="16" gravity="center" />
                                        <text w="50" h="50" bg="#999400D3" />
                                    </vertical>
                                    <vertical id="白云" w="50" h="90" clickable="true" bg="?android:attr/selectableItemBackground">
                                        <text text="白云" textColor="#FFFFFF" textSize="16sp" marginTop="16" gravity="center" />
                                        <text w="50" h="50" bg="#99FFFFFF" />
                                    </vertical>
                                    <vertical id="残阳" w="50" h="90" clickable="true" bg="?android:attr/selectableItemBackground">
                                        <text text="残阳" textColor="#FFFFFF" textSize="16sp" marginTop="16" gravity="center" />
                                        <text w="50" h="50" bg="#99FF0000" />
                                    </vertical>
                                </horizontal>
                                <horizontal>
                                    <vertical id="樱花" w="50" h="90" clickable="true" bg="?android:attr/selectableItemBackground">
                                        <text text="樱花" textColor="#FFFFFF" textSize="16sp" marginTop="16" gravity="center" />
                                        <text w="50" h="50" bg="#99feeeed" />
                                    </vertical>
                                    <vertical id="深海" w="50" h="90" clickable="true" bg="?android:attr/selectableItemBackground">
                                        <text text="深海" textColor="#FFFFFF" textSize="16sp" marginTop="16" gravity="center" />
                                        <text w="50" h="50" bg="#990066ff" />
                                    </vertical>
                                    <vertical id="橙子" w="50" h="90" clickable="true" bg="?android:attr/selectableItemBackground">
                                        <text text="橙子" textColor="#FFFFFF" textSize="16sp" marginTop="16" gravity="center" />
                                        <text w="50" h="50" bg="#99ff6600" />
                                    </vertical>
                                    <vertical id="月光" w="50" h="90" clickable="true" bg="?android:attr/selectableItemBackground">
                                        <text text="月光" textColor="#FFFFFF" textSize="16sp" marginTop="16" gravity="center" />
                                        <text w="50" h="50" bg="#99ffff00" />
                                    </vertical>
                                    <vertical id="淡然" w="50" h="90" clickable="true" bg="?android:attr/selectableItemBackground">
                                        <text text="淡然" textColor="#FFFFFF" textSize="16sp" marginTop="16" gravity="center" />
                                        <text w="50" h="50" bg="#99ccffff" />
                                    </vertical>
                                    <vertical id="咖啡" w="50" h="90" clickable="true" bg="?android:attr/selectableItemBackground">
                                        <text text="咖啡" textColor="#FFFFFF" textSize="16sp" marginTop="16" gravity="center" />
                                        <text w="50" h="50" bg="#99cc6600" />
                                    </vertical>
                                    <vertical id="雨后" w="50" h="90" clickable="true" bg="?android:attr/selectableItemBackground">
                                        <text text="雨后" textColor="#FFFFFF" textSize="16sp" marginTop="16" gravity="center" />
                                        <text w="50" h="50" bg="#99c0ffee" />
                                    </vertical>
                                </horizontal>
                                <input id="selfcolor" textStyle="bold" text="请输入自定义16进制颜色码(长按清除文本)" textColor="#FFFFFF" />
                                <horizontal gravity="center">
                                    <button text="提交颜色码" id="commitcolor" backgroundTint="#00000000" textColor="#FFFFFF" />
                                    <button text="查看颜色码" id="viewcolor" backgroundTint="#00000000" textColor="#FFFFFF" />
                                </horizontal>
                                <text text="从网页下载的图片:" textColor="#FFFFFF" />
                                <input id="inurl" textStyle="bold" text="请复制粘贴网络图片链接(长按清除文本)" textColor="#FFFFFF" />
                                <button text="下载" id="downloadimg" backgroundTint="#00000000" textColor="#FFFFFF" />
                                <text text="从本地上传的图片:" textColor="#FFFFFF" />
                                <input id="upurl" textStyle="bold" text="请复制粘贴本地图片链接(长按清除文本)" textColor="#FFFFFF" />
                                <button text="上传" id="uploadimg" backgroundTint="#00000000" textColor="#FFFFFF" />
                            </vertical>
                        </card>
                    </vertical>
                    <card bg="#55000000">
                        <vertical>
                            <text id="intervaltext" text="{{'当前间隔值'+storage.get('interval')}}" textColor="#FFFFFF" />
                            <input id="valueofinterval" textStyle="bold" text="请输入通道间隔值(长按清除文本)" textColor="#FFFFFF" />
                            <button text="确定间隔值" id="commitinterval" backgroundTint="#00000000" textColor="#FFFFFF" />
                            <text text="当前要处理的图片:" textColor="#FFFFFF" />
                            <img id="imgbg" src="file://res/1.png" />
                            <text text="图片处理较慢，请耐心等待!" />
                            <button text="处理图片" id="proimg" />
                        </vertical>
                    </card>
                </vertical>
            </scroll>
        </frame>
    )
    ui.statusBarColor("#000000");
    //图像处理按钮点击监听
    ui.proimg.on("click", () => {
        img = images.read("./res/1.png") //读图，要处理的目标图片
        pointcolor = "#" + (colors.toString(images.pixel(img, img.getWidth() / 10, img.getHeight() / 10))).substr(3);//取屏幕长宽各位十分之一处的点，获得该点颜色值，截取字符串去除透明度(可以不去除)
        bimg = images.toBytes(img); //将图片转换为字节数组
        bmimg = android.graphics.BitmapFactory.decodeByteArray(bimg, 0, bimg.length); //字节数组转bitmat
        desbmimg = bmimg.copy(android.graphics.Bitmap.Config.ARGB_8888, true); //解决E: Wrapped java.lang.IllegalStateException，不能直接在原图上修改
        //二值化图像
        dimg = images.interval(img, pointcolor, storage.get("interval"));//调用颜色通道间隔值函数，二值化图像
        images.save(dimg, "./res/2.png") //保存图
        bdimg = images.toBytes(dimg); //将图片转换为字节数组
        bmdimg = android.graphics.BitmapFactory.decodeByteArray(bdimg, 0, bdimg.length); //字节数组转bitmat
        desbmdimg = bmdimg.copy(android.graphics.Bitmap.Config.ARGB_8888, true); //解决E: Wrapped java.lang.IllegalStateException
        //图像处理
        bmh = bmdimg.getHeight();//获取图片高
        bmw = bmdimg.getWidth();//获取图片宽
        //遍历二值化图片所有像素点，如果该点为白色，则在原图上对应坐标点替换为目标背景色
        for (i = 0; i < bmw; i++) {
            for (j = 0; j < bmh; j++) {
                //十分之一处点是选取的判断点，肯定是背景，不可能有人的脸盘子占了证件照超过十分之九！
                if (desbmdimg.getPixel(i, j) == desbmdimg.getPixel(bmw / 10, bmh / 10)) {
                    desbmimg.setPixel(i, j, colors.parseColor(storage.get("color")));//重置目标点背景色，从存储里面读取颜色字符串，再转颜色整数
                }
            }
        }
        outputSteam = java.io.ByteArrayOutputStream();//调用Java字节数组输出流
        desbmimg.compress(android.graphics.Bitmap.CompressFormat.PNG, 100, outputSteam);//调用bitmap的compress压缩函数，把图片以百分百的质量，用PNG的格式压缩到输出流里面
        newbyte = outputSteam.toByteArray();//输出流转字节数组
        newimg = images.fromBytes(newbyte);//字节数组转图片
        images.save(newimg, "./res/3.png");//保存图片
        outputSteam.close();//关闭输出流
        imgbg();
    });
    //背景色选择点击监听器
    ui.真爱.on("click", () => {
        toast("如果真爱有颜色，那一定是蓝色！")
        storage.put("color", "#00BFFF");;
        ui.targetcolor.setBackgroundColor(colors.parseColor("#99" + storage.get("color").substr(1)))
    })
    ui.纯黑.on("click", () => {
        storage.put("color", "#000000");;
        ui.targetcolor.setBackgroundColor(colors.parseColor("#99" + storage.get("color").substr(1)))
    })
    ui.骚粉.on("click", () => {
        storage.put("color", "#FFB6C1");;
        ui.targetcolor.setBackgroundColor(colors.parseColor("#99" + storage.get("color").substr(1)))
    })
    ui.原谅.on("click", () => {
        storage.put("color", "#3CB371");;
        ui.targetcolor.setBackgroundColor(colors.parseColor("#99" + storage.get("color").substr(1)))
    })
    ui.真爱.on("click", () => {
        storage.put("color", "#00BFFF");;
        ui.targetcolor.setBackgroundColor(colors.parseColor("#99" + storage.get("color").substr(1)))
    })
    ui.基佬.on("click", () => {
        storage.put("color", "#9400D3");;
        ui.targetcolor.setBackgroundColor(colors.parseColor("#99" + storage.get("color").substr(1)))
    })
    ui.白云.on("click", () => {
        storage.put("color", "#FFFFFF");;
        ui.targetcolor.setBackgroundColor(colors.parseColor("#99" + storage.get("color").substr(1)))
    })
    ui.残阳.on("click", () => {
        storage.put("color", "#FF0000");;
        ui.targetcolor.setBackgroundColor(colors.parseColor("#99" + storage.get("color").substr(1)))
    })
    ui.樱花.on("click", () => {
        storage.put("color", "#feeeed");;
        ui.targetcolor.setBackgroundColor(colors.parseColor("#99" + storage.get("color").substr(1)))
    })
    ui.深海.on("click", () => {
        storage.put("color", "#0066ff");;
        ui.targetcolor.setBackgroundColor(colors.parseColor("#99" + storage.get("color").substr(1)))
    })
    ui.橙子.on("click", () => {
        storage.put("color", "#ff6600");;
        ui.targetcolor.setBackgroundColor(colors.parseColor("#99" + storage.get("color").substr(1)))
    })
    ui.月光.on("click", () => {
        storage.put("color", "#ffff00");;
        ui.targetcolor.setBackgroundColor(colors.parseColor("#99" + storage.get("color").substr(1)))
    })
    ui.淡然.on("click", () => {
        storage.put("color", "#ccffff");;
        ui.targetcolor.setBackgroundColor(colors.parseColor("#99" + storage.get("color").substr(1)))
    })
    ui.咖啡.on("click", () => {
        storage.put("color", "#cc6600");;
        ui.targetcolor.setBackgroundColor(colors.parseColor("#99" + storage.get("color").substr(1)))
    })
    ui.雨后.on("click", () => {
        storage.put("color", "#c0ffee");;
        ui.targetcolor.setBackgroundColor(colors.parseColor("#99" + storage.get("color").substr(1)))
    })
    //网址输入框长按监听
    ui.inurl.on("long_click", () => {
        ui.inurl.setText("");
    });
    //自定义颜色通道间隔值输入框长按监听
    ui.valueofinterval.on("long_click", () => {
        ui.valueofinterval.setText("");
    });
    //提交自定义颜色通道间隔值点击监听
    ui.commitinterval.on("click", () => {
        storage.put("interval", Number(ui.valueofinterval.getText()));
        ui.intervaltext.setText('当前间隔值' + storage.get('interval'));
    })
    //自定义颜色码输入框长按监听
    ui.selfcolor.on("long_click", () => {
        ui.selfcolor.setText("#");
    });
    //提交自定义颜色点击监听
    ui.commitcolor.on("click", () => {
        storage.put("color", ui.selfcolor.getText().toString());
        ui.targetcolor.setBackgroundColor(colors.parseColor("#99" + storage.get("color").substr(1)))
    })
    //查看颜色码监听，调用浏览器打开颜色码查看网址
    ui.viewcolor.on("click", () => {
        i = app.intent({
            action: "VIEW",
            data: "https://www.cnblogs.com/planetwithpig/p/11663209.html"
        });
        app.startActivity(i);
    })
    //本地URL长按监听
    ui.upurl.on("long_click", () => {
        ui.upurl.setText("");
    });
    //更换证件照图片显示点击监听
    ui.imgbg.on("click", () => {
        ui.imgbg.setSource("file://res/1.png");
    });
    //下载网络图片
    ui.downloadimg.on("click", () => {
        url = ui.inurl.getText().toString();
        threads.start(function() {
            var res = http.get(url);
            if (res.statusCode != 200) {
                toast("请求失败");
            }
            files.writeBytes("./res/1.png", res.body.bytes());
            toast("下载成功");
            className("android.widget.ImageView").findOne().click();
        })
    });
    //上传本地图片
    ui.uploadimg.on("click", () => {
        url = ui.upurl.getText().toString();
        threads.start(function() {
            img = images.read(url);
            images.save(img, "./res/1.png")
            toast("上传成功");
            className("android.widget.ImageView").findOne().click();
        })
    });
}

function imgbg() {
    ui.layout(
        <frame>
            <ScrollView>
                <vertical>
                    <text text="修改前的图片：" />
                    <img src="file://res/1.png" />
                    <text text="二值化的图片：" />
                    <img src="file://res/2.png" />
                    <text text="修改后的图片:" />
                    <img src="file://res/3.png" />
                    <text  text="{{'当前文件名:'+storage.get('filepath')}}"/>
                    <input id="valueofinterval" textStyle="bold" text="请输入保存文件名.文件类型(如,证件照.png,长按归清除文本)" textColor="#000000" />
                    <button id="saveimg" text="保存图片至根目录"/>
                    <button id="back" text="返回继续处理"/>
                </vertical>
            </ScrollView>
        </frame>
    )
    ui.saveimg.on("click", () => {
        img = images.read("./res/3.png");
        images.save(img,"/sdcard/"+storage.get("filepath"));
        toast("保存成功!");
    });
    ui.back.on("click", () => {
        mainui();
    })
}
storage = storages.create("setting");
storage.put("color", "#ff0000");
storage.put("interval", 30);
storage.put("filepath", "处理后证件照.png")
mainui();